package com.htbb.controller.personalInsure;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.htbb.common.base.PropertiesConfig;
import com.htbb.common.base.Result;
import com.htbb.common.base.exception.BizException;
import com.htbb.common.msgBodyAdvice.CryptionType;
import com.htbb.model.personalInsure.ReqOfIsuChk;
import com.htbb.order.entity.InsureOrder;
import com.htbb.order.entity.TcIsu;
import com.htbb.order.interfaces.TcOrderService;
import com.htbb.order.utils.DateUtil;
import com.htbb.pay.entity.TcPay;
import com.htbb.pay.interfaces.TcPayService;
import com.htbb.service.personalInsure.IsuBizService;

/**
 * @date 2018-08-25
 * @description 业务流程接口
 * @author wuyongde
 *
 */
@Controller
@RequestMapping(value = "personInsure")
public class IsuBizController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	private static final String epolicPathPrefix = PropertiesConfig.nfsFile;

	@Autowired
	private IsuBizService isuBizService;
	@Autowired
	private TcOrderService tcOrderService;
	@Autowired
	private TcPayService tcPayService;
	
	/**
	 * 申请投保接口
	 * @param reqOfIsuChk 请求数据
	 * @return
	 * @throws Exception
	 */
	@CryptionType
	@ResponseBody
	@RequestMapping(value = "submitOrder", method = { RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object submitOrder(@RequestBody ReqOfIsuChk reqOfIsuChk) throws Exception {
			
		return new Result<Object>(isuBizService.commitOrder(reqOfIsuChk));

	}
	

	/**
	 * 投保信息查询
	 * @param request
	 * @param response
	 * @param hbCode
	 * @return
	 * @throws Exception
	 */
	@CryptionType
	@ResponseBody
	@RequestMapping(value = "queryOrder", method = { RequestMethod.POST }, produces = "application/json;charset=UTF-8")
	public Object toOrderQuery(@RequestBody Map<String, String> reqParams) throws Exception {

		String hbCode = reqParams.get("hbCode"), orderId = reqParams.get("orderId"), isuNum = reqParams.get("isuNum");
		if(StringUtils.isEmpty(hbCode) && StringUtils.isEmpty(orderId) && StringUtils.isEmpty(isuNum)){
			throw new BizException("缺少必要参数");
		}
		
		TcIsu insureOrder = isuBizService.queryTcAcctIsu(hbCode,orderId,isuNum);
		Result<Object> result = new Result<Object>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (insureOrder == null){
			throw new BizException("无效的单号,没有找到订单记录");
		}
		Integer isuStatus = insureOrder.getIsuStatus();
		resultMap.put("orderStatus",isuStatus);
		resultMap.put("createDate", DateUtil.dateToString(insureOrder.getIsuDate(), DateUtil.PATTERN_CLASSICAL));
		
		if ("2".equals(isuStatus) || "3".equals(isuStatus)) {
			resultMap.put("epolicyNum", insureOrder.getIsuNum());
			resultMap.put("epolicPath", epolicPathPrefix + insureOrder.getOrderId() + ".pdf");
		} 

		resultMap.put("effectiveDate", insureOrder.getDateStart());
		resultMap.put("isuDtEnd", insureOrder.getDateEnd());
		resultMap.put("orderPrice", insureOrder.getIsuAmt());
		resultMap.put("prodName", insureOrder.getProdName());
		resultMap.put("custEmail", insureOrder.getEmail());
		resultMap.put("custPhone", insureOrder.getCustPhone());
		resultMap.put("custName", insureOrder.getCustName());
		resultMap.put("isuName", insureOrder.getIsuName());
		resultMap.put("isuSsn", insureOrder.getIsuIdNum());
		result.setModel(resultMap);
		return result;
	}

	/**
	 * 支付查询
	 */
	@CryptionType
	@ResponseBody
	@RequestMapping(value = "queryPay", method = { RequestMethod.POST,RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public Object toPayQuery(@RequestBody Map<String, String> reqParams) throws Exception {
		
		String payNo = reqParams.get("payNo");
		TcPay tcOrderPay = tcPayService.selectByKey(payNo);
		Result<Object> result = new Result<Object>();
		Map<String,Object> resultMap = new HashMap<>();
		if(tcOrderPay != null){
			Integer status = tcOrderPay.getPayStatus()==null?0:tcOrderPay.getPayStatus();
			//0待支付，1支付成功，2支付失败
			if(status == 0){
				resultMap.put("payStatus", 3);  //0支付失败 1支付成功 2该支付单号不存在 3未支付
			}else if(status == 2){
				resultMap.put("payStatus", 0);
			}else if(status == 1){
				resultMap.put("payStatus", 1);
			}
			resultMap.put("orderNo", tcOrderPay.getOrderId());
			resultMap.put("price", tcOrderPay.getPayAmt());
		}else{
			resultMap.put("payStatus", 2);
		}
		result.setModel(resultMap);
		return result;
	}
	
	/**
	 * 根据partnerCode和手机号查询出该用户在改渠道下的所有订单和保单
	 */
	@CryptionType
	@ResponseBody
	@RequestMapping(value = "queryPersonOrders", method = { RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	public Object queryPartnerOrder(@RequestBody Map<String, String> reqParams){
		
		List<InsureOrder> ioList = isuBizService.queryPartnerOrder(reqParams.get("custPhone"),reqParams.get("partnerCode"));
		
		return new Result<Object>(ioList);
	}
	
	/**
	 * 退保申请接口（160停诊险）
	 * @param reqOfIsuChk
	 * @return
	 * @throws Exception
	 */
	@CryptionType
	@ResponseBody
	@RequestMapping(value = "surrender", method = { RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	public Object toSurrender(@RequestBody Map<String, String> reqParams)
			throws Exception {
			String hbCode = reqParams.get("hbCode");
			String isuSsn = reqParams.get("isuSsn");
			if(StringUtils.isEmpty(hbCode) || StringUtils.isEmpty(isuSsn)){
				throw new BizException("参数缺失（hbCode或isuSsn）");
			}
			
			return new Result<Object>(isuBizService.surrender(hbCode, isuSsn));
	}
	
}






